<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="java.io.PrintWriter, java.sql.ResultSet,
    		cn.edu.nju.software.model.administrator.MemberManager,
    		cn.edu.nju.software.model.administrator.DisplayManager"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
	String administratorName = (String) application.getAttribute("administratorName");
	if (administratorName == null || administratorName.equals("")) {
		PrintWriter printWriter = response.getWriter();
		printWriter.println("<script>alert('Please login first!');" + 
							"window.location.href='index.jsp';</script>");
	}
%>

<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<link type="text/css" rel="stylesheet" href="../css/administrator/statistics.css" />
	<link type="text/css" rel="stylesheet" href="../css/administrator/menu.css" />
	<link type="text/css" rel="stylesheet" href="../css/administrator/footer.css" />
	<title>统计分析</title>
</head>

<body>
	<div id="menu">
		<a href="administrator.jsp"><img src="../images/administrator/menu/menu_homepage.png" alt="首页" /></a>
		<a href="member.jsp"><img src="../images/administrator/menu/menu_member.png" alt="会员管理" /></a>
		<a href="display.jsp"><img src="../images/administrator/menu/menu_display.png" alt="放映管理" /></a>
		<a href="ticket.jsp"><img src="../images/administrator/menu/menu_ticket.png" alt="售票" /></a>
		<a href="statistics.jsp"><img src="../images/administrator/menu/menu_statistics.png" alt="统计分析" /></a>
		<a href="information.jsp"><img src="../images/administrator/menu/menu_information.png" alt="信息" /></a>
		<a href="index.jsp"><img src="../images/administrator/menu/menu_logout.png" alt="注销" /></a>
	</div>
	
	<div id="content">
		
		<div id="card">
			<%
			MemberManager memberManager = new MemberManager();
			%>
			<div id="status" class="card_cell">
				<div class="cell_title">会员卡状态</div>
				<div class="statistics_cell">
					<table>
						<tr class="color_row">
							<td>状态</td>
							<td>激活</td>
							<td>未激活</td>
						</tr>
						<tr>
							<td>人数</td>
							<td><%= memberManager.usableCount() %></td>
							<td><%= memberManager.unusableCount() %></td>
						</tr>
					</table>
				</div>
			</div>
			
			<div id="gendar" class="card_cell">
				<div class="cell_title">性别统计</div>
				<div class="statistics_cell">
					<table>
						<tr class="color_row">
							<td>性别</td>
							<td>男</td>
							<td>女</td>
						</tr>
						<tr>
							<td>人数</td>
							<td><%= memberManager.maleCount() %></td>
							<td><%= memberManager.femaleCount() %></td>
						</tr>
					</table>
				</div>
			</div>
			
			<div id="age" class="card_cell">
				<div class="cell_title">年龄统计</div>
				<div class="statistics_cell">
					<table>
						<tr class="color_row">
							<td>年龄</td>
							<td>50后</td>
							<td>60后</td>
							<td>70后</td>
							<td>80后</td>
							<td>90后</td>
							<td>00后</td>
						</tr>
						<tr>
							<td>人数</td>
							<td><%= memberManager.a5Count() %></td>
							<td><%= memberManager.a6Count() %></td>
							<td><%= memberManager.a7Count() %></td>
							<td><%= memberManager.a8Count() %></td>
							<td><%= memberManager.a9Count() %></td>
							<td><%= memberManager.a0Count() %></td>
						</tr>
					</table>
				</div>
			</div>
		</div>
		
		<div id="display">
			<div class="cell_title">销售情况</div>
			<div class="statistics_cell">
				<table>
					<tr>
						<th>放映室</th>
						<th>影片</th>
						<th>时间</th>
						<th>票价</th>
						<th>容纳人数</th>
						<th>剩余票数</th>
						<th>上座率</th>
					</tr>
					<%
					DisplayManager displayManager = new DisplayManager();
					ResultSet rs = displayManager.fetchAllDisplays();
					if (rs != null) {
						while (rs.next()) {
							String hall = rs.getString(2);
							String movie = rs.getString(3);
							String time = rs.getString(4);
							String[] times = time.split("[/:-]");
							String minute = times[3];
							if (minute.length() == 1) {
								minute = "0" + minute;
							}
							String time_str = times[0] + "月 " + times[1] + "日 " + times[2] + ":" + minute;
							
							int capacity = rs.getInt(5);
							int surplus = rs.getInt(7);
							int price = rs.getInt(8);
							double loadFactor = (capacity - surplus) / (capacity * 1.0) * 100;
							loadFactor = Math.round(loadFactor * 100) / 100.0;
							
							int row = rs.getRow();
							if (row % 2 == 0) {
								%>
								<tr class="color_row">
									<td><%= hall %></td>
									<td><%= movie %></td>
									<td><%= time_str %></td>
									<td><%= price %></td>
									<td><%= capacity %></td>
									<td><%= surplus %></td>
									<td><%= loadFactor %>%</td>
								</tr>
								<%
							} else {
								%>
								<tr>
									<td><%= hall %></td>
									<td><%= movie %></td>
									<td><%= time_str %></td>
									<td><%= price %></td>
									<td><%= capacity %></td>
									<td><%= surplus %></td>
									<td><%= loadFactor %>%</td>
								</tr>
								<%
							}
						}
					}
					%>
				</table>
			</div>
		</div>
		
	</div>
	
	<div id="footer">
		<p>
			Copyright &copy; <a href="mailto:zyy08@software.nju.edu.cn">张迎亚</a>·南京大学·软件学院·2011春节
		</p>
	</div>
	
</body>

</html>